package com.hnqcgczy.supplier.dao.impl;

import com.hnqcgczy.supplier.dao.ShareholderDao;
import com.hnqcgczy.supplier.db.MyTransaction;
import com.hnqcgczy.supplier.utils.BaseDao;
import com.hnqcgczy.supplier.vo.Shareholder;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ShareholderDaoImpl extends BaseDao implements ShareholderDao {
    public ShareholderDaoImpl(MyTransaction myTransaction) {
        super(myTransaction);
    }

    @Override
    public int add(Shareholder shareholder) throws SQLException {
        String sql = "INSERT INTO `t_shareholder`(`shareholder_id`, `company_id`, `shareholder_name`, `shareholding_ratio`) VALUES (null, ?, ?, ?)";
        int i = super.update(sql, shareholder.getCompanyId(), shareholder.getShareholderName(), shareholder.getShareholdingRatio());
        super.closeResource();
        return i;
    }

    @Override
    public int update(Shareholder shareholder) throws SQLException {
        String sql = "UPDATE `t_shareholder` SET `company_id` = ?, `shareholder_name` = ?, `shareholding_ratio` = ? WHERE `shareholder_id` = ?";
        int i = super.update(sql, shareholder.getCompanyId(), shareholder.getShareholderName(), shareholder.getShareholdingRatio(), shareholder.getShareholderId());
        super.closeResource();
        return i;
    }

    @Override
    public int delete(Integer shareholderId) throws SQLException {
        String sql = "delete from `t_shareholder`  WHERE `shareholder_id` = ?";
        int i = super.update(sql, shareholderId);
        super.closeResource();
        return i;
    }

    @Override
    public Shareholder findOne(Integer shareholderId) throws SQLException {
        String sql = "select shareholder_id,company_id,shareholder_name,shareholding_ratio from `t_shareholder` where `shareholder_id` = ?";
        ResultSet rs = super.query(sql, shareholderId);
        if (rs != null) {
            while (rs.next()) {
                Shareholder shareholder = new Shareholder();
                shareholder.setShareholderId(rs.getInt("shareholder_id"));
                shareholder.setCompanyId(rs.getInt("company_id"));
                shareholder.setShareholderName(rs.getString("shareholder_name"));
                shareholder.setShareholdingRatio(rs.getString("shareholding_ratio"));
                return shareholder;
            }
        }

        return null;
    }

    @Override
    public List<Shareholder> findByLike(Shareholder shareholder, int start, int size) throws SQLException {
        List<Shareholder> lists = null;
        StringBuffer sb = new StringBuffer("select shareholder_id,company_id,shareholder_name,shareholding_ratio from `t_shareholder` where 1=1 ");
        if (shareholder != null) {
            //shareholder_name
            if (shareholder.getShareholderName() != null && !"".equals(shareholder.getShareholderName())) {
                sb.append(" and shareholder_name like '%").append(shareholder.getShareholderName()).append("%'");
            }
        }

        //
        String sql = sb.toString();
        System.out.println(sql);
        ResultSet rs = super.query(sql);
        if (rs != null) {
            lists = new ArrayList<>();
            while (rs.next()) {
                Shareholder temp = new Shareholder();
                temp.setShareholderId(rs.getInt("shareholder_id"));
                temp.setCompanyId(rs.getInt("company_id"));
                temp.setShareholderName(rs.getString("shareholder_name"));
                temp.setShareholdingRatio(rs.getString("shareholding_ratio"));
                lists.add(temp);
                temp = null;
            }
        }
        super.closeResource();
        return lists;
    }

    @Override
    public int totals(Shareholder shareholder) throws SQLException {
        StringBuffer sb = new StringBuffer("select count(*) totals from `t_shareholder` where 1=1 ");
        if (shareholder != null) {
            //shareholder_name
            if (shareholder.getShareholderName() != null && !"".equals(shareholder.getShareholderName())) {
                sb.append(" and shareholder_name like '%").append(shareholder.getShareholderName()).append("%'");
            }
        }

        //
        String sql = sb.toString();
        ResultSet rs = super.query(sql);
        int totals = 0;
        if (rs != null) {

            while (rs.next()) {

                totals = rs.getInt("totals");
            }
        }
        super.closeResource();
        return totals;
    }
}
